package com.fb;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import com.dev.ArchiveListTab;
import com.dev.CEditorTab;
import com.dev.DocumentFile;
import com.dev.FileProvider;
import com.dev.HtmlBrowser;
import com.dev.HtmlIndentEditor;
import com.dev.IconView;
import com.dev.ImageManipulationTab;
import com.dev.IndentEditor;
import com.dev.JavaEditorPage;
import com.dev.JavaEditorTab;
import com.dev.MainActivity;
import com.dev.QueryOpenFile;
import com.dev.R;
import com.dev.TabBar;
import com.dev.TabData;
import com.log;
import java.io.File;
import java.io.FileReader;
import java.net.URI;

public class DirectoryListView extends View {
    FileBrowserPage fbp;
    public int startX;
    public int startY;
    public int position;
    public int positionSelected;
    public int itemHeight;
	public int itemIdentLen;
    public int longClickLim=400;
    float iconWidth=5/4f;
    int positionPressed=-1;
    int positionSaved;
    int startXMax;
    int itemTextPaddingLeft=12;
    int nameTextSize;
    int nameTextBaseline;
    int infoTextSize;
    int infoTextBaseline;
    int infoTextColor=0xff888888;
    int dividerWidth=2;
    DirectoryListView(FileBrowserPage fbp){
        super(fbp.ma);
        this.fbp=fbp;
        setFocusable(true);
        itemHeight=(int)(27*fbp.ma.dp);
        itemIdentLen=itemHeight*5/4;
        infoTextSize=(int)(10*fbp.ma.dp);
        fbp.itemlp=new ViewGroup.LayoutParams(-1,itemHeight);
        fingerMovedLen=(int)(4*fbp.ma.dp);
        nameTextSize=(int)(11*fbp.ma.dp);
        
    }
    void atIconClick(int pos){
        if(fbp.selected==null){
            fbp.selected=new boolean[fbp.willshowLen];
            fbp.selected[pos]=true;
            fbp.box.setVisibility(VISIBLE);
        }else if(fbp.selected[pos]){
            fbp.selected[pos]=false;
            for(int i=0;true;i++){
                if(i<fbp.selected.length){
                    if(fbp.selected[i]){
                        break;
                    }
                }else{
                    fbp.selected=null;
                    fbp.box.setVisibility(GONE);
                    break;
                }
            }
        }else{
            fbp.selected[pos]=true;
            fbp.box.setVisibility(VISIBLE);
        }
        invalidate();
    }
    void atItemClick(int pos){
        if(fbp.ma.queryOpenFile==null)fbp.ma.queryOpenFile=new QueryOpenFile(fbp.ma);
        MainActivity ma=fbp.ma;
        if(fbp.selected==null){
            if(fbp.willshowSorted==null){
                File file=fbp.willshow[pos];
                if(file.isFile()){
                    if(file.canRead()){
                        String name=file.getName();
                        int dot=name.lastIndexOf('.');
                        if(dot>0){
                            String type=name.substring(dot+1);
                            if(type.equals("zip")){
                                ArchiveListTab alt=new ArchiveListTab(ma);
                                TabData.ArchiveViewer av=new TabData.ArchiveViewer();
                                av.src=file;
                                alt.av=av;
                                alt.setOnClickListener(ma.gui.TabOnClick);

                                alt.setText(file.getName());
                                ma.gui.tabs.addView(alt,TabBar.tablp);
                                ma.gui.TabOnClick.onClick(alt);
                                ma.gui.saveAllTab();
                                return;
                            }else if(type.equals("mp3")||type.equals("m4a")||type.equals("mp4")){
                                try{
                                    if(fbp.hb==null){fbp.hb=new HtmlBrowser(ma);fbp.addView(fbp.hb,fbp.pageLayoutParams);}
                                    fbp.file=file;
                                    ma.gui.ivInto.setVisibility(View.GONE);
                                    ma.gui.ivQuit.setVisibility(View.VISIBLE);
                                    fbp.dlv.setVisibility(View.GONE);
                                    fbp.fn.setVisibility(View.GONE);
                                    fbp.toolsbar.setVisibility(View.GONE);
                                    fbp.hb.setVisibility(View.VISIBLE);
                                    fbp.hb.webView.loadUrl("file://"+file.getAbsolutePath());
                                    fbp.dyn=3;
                                }catch(Exception e){log.alert(e.toString());}
                                return;
                            }else if(type.equals("jpg")||type.equals("jpeg")||type.equals("png")){
                                try{
                                    ImageManipulationTab imt=new ImageManipulationTab(ma);
                                    TabData.ImageManipulation im=new TabData.ImageManipulation();
                                    im.src=file;
                                    imt.im=im;
                                    imt.setOnClickListener(ma.gui.TabOnClick);

                                    imt.setText(file.getName());
                                    ma.gui.tabs.addView(imt,TabBar.tablp);
                                    ma.gui.TabOnClick.onClick(imt);
                                    ma.gui.saveAllTab();
                                }catch(Exception e){log.alert(e.toString());}
                                return;
							}
                        }
                        if(file.length()<MainActivity.FILE_LENGTH_LIM){
                            if(dot<=0){
                                ma.queryOpenFile.openInNewTab=false;
                                ma.queryOpenFile.destination=file;
                                ma.queryOpenFile.show(fbp);
                            }else{
                                String type=name.substring(dot+1);
                                if(type.equals("txt")){
                                    
                                    ma.gui.ivInto.setVisibility(View.GONE);
                                    ma.gui.ivQuit.setVisibility(View.VISIBLE);
                                    ma.gui.ivInfo.setVisibility(View.VISIBLE);
                                    ma.gui.ivRW.setImageResource(R.drawable.ic_lead_pencil);
                                    ma.gui.ivRW.setVisibility(View.VISIBLE);
                                    ma.gui.ivSave.setColorFilter(IconView.unable);
                                    ma.gui.ivSave.setVisibility(View.VISIBLE);
                                    ma.gui.ivRefresh.setColorFilter(IconView.unable);
                                    ma.gui.ivRefresh.setVisibility(View.VISIBLE);
                                    ma.gui.status.setText(file.getAbsolutePath());
                                    ma.gui.ebi.setVisibility(VISIBLE);
                                    
                                
                                    StringBuilder sb=new StringBuilder();
                                    try{
                                        FileReader fr=new FileReader(file);
                                        int ii=fr.read();
                                        while(ii>=0){
                                            sb.append((char)ii);
                                            ii=fr.read();
                                        }
                                        if(fbp.ie==null){
                                            fbp.ie=new IndentEditor(ma);
                                            fbp.addView(fbp.ie,fbp.pageLayoutParams);
                                        }
                                        ma.gui.ebi.to=fbp.ie;
                                        fbp.ie.letText(sb.toString().toCharArray());
                                        fbp.ie.setVisibility(VISIBLE);
                                        fbp.dyn=1;
                                    }catch(Exception e){
                                        log.alert(e.toString());
                                    }
                                }else if(type.equals("html")||type.equals("htm")){
                                    fbp.dlv.setVisibility(View.GONE);
                                    fbp.fn.setVisibility(View.GONE);
                                    fbp.toolsbar.setVisibility(View.VISIBLE);
                                    ma.gui.ivInto.setVisibility(View.GONE);
                                    ma.gui.ivQuit.setVisibility(View.VISIBLE);
                                    ma.gui.ivInfo.setVisibility(View.VISIBLE);
                                    ma.gui.ivRW.setImageResource(R.drawable.ic_lead_pencil);
                                    ma.gui.ivRW.setVisibility(View.VISIBLE);
                                    ma.gui.ivSave.setColorFilter(IconView.unable);
                                    ma.gui.ivSave.setVisibility(View.VISIBLE);
                                    ma.gui.ivRefresh.setColorFilter(IconView.unable);
                                    ma.gui.ivRefresh.setVisibility(View.VISIBLE);
                                    ma.gui.status.setText(file.getAbsolutePath());
                                    ma.gui.ivPR.setVisibility(View.VISIBLE);
                                    ma.gui.ivPR.setImageResource(R.drawable.ic_play);
                                    ma.gui.ebi.setVisibility(VISIBLE);
                                    fbp.editorCanSave=false;
                                    fbp.file=file;
                                    fbp.dyn=2;
                                    if(ma.shr.getBoolean("showInfoBar",false)){
                                        ma.gui.status.setVisibility(View.VISIBLE);
                                    }else{
                                        ma.gui.ivInfo.setVisibility(View.VISIBLE);
                                    }
                                    StringBuilder sb=new StringBuilder();
                                    try{
                                        FileReader fr=new FileReader(file);
                                        int ii=fr.read();
                                        while(ii>=0){
                                            sb.append((char)ii);
                                            ii=fr.read();
                                        }
                                        if(fbp.hie==null){fbp.hie=new HtmlIndentEditor(ma);fbp.addView(fbp.hie,fbp.pageLayoutParams);}
                                        fbp.hie.letText(sb.toString().toCharArray());
                                        fbp.hie.setVisibility(View.VISIBLE);
                                    }catch(Exception e){
                                        log.alert(e.toString());
                                    }
                                }else if(type.equals("svg")){
                                    fbp.file=file;
                                    ma.gui.ivInto.setVisibility(View.GONE);
                                    ma.gui.ivQuit.setVisibility(View.VISIBLE);
                                    ma.gui.ivInfo.setVisibility(View.VISIBLE);
                                    ma.gui.ivRW.setImageResource(R.drawable.ic_lead_pencil);
                                    ma.gui.ivRW.setVisibility(View.VISIBLE);
                                    ma.gui.ivSave.setColorFilter(IconView.unable);
                                    ma.gui.ivSave.setVisibility(View.VISIBLE);
                                    ma.gui.ivRefresh.setColorFilter(IconView.unable);
                                    ma.gui.ivRefresh.setVisibility(View.VISIBLE);
                                    ma.gui.status.setText(file.getAbsolutePath());
                                    ma.gui.ivPR.setVisibility(View.VISIBLE);
                                    ma.gui.ivPR.setImageResource(R.drawable.ic_play);
                                    ma.gui.ebi.setVisibility(VISIBLE);
                                    fbp.dlv.setVisibility(View.GONE);
                                    fbp.fn.setVisibility(View.GONE);
                                    fbp.toolsbar.setVisibility(View.GONE);
                                    fbp.editorCanSave=false;
                                    fbp.dyn=4;
                                    if(ma.shr.getBoolean("showInfoBar",false)){
                                        ma.gui.status.setVisibility(View.VISIBLE);
                                    }else{
                                        ma.gui.ivInfo.setVisibility(View.VISIBLE);
                                    }
                                    StringBuilder sb=new StringBuilder();
                                    try{
                                        FileReader fr=new FileReader(file);
                                        int ii=fr.read();
                                        while(ii>=0){
                                            sb.append((char)ii);
                                            ii=fr.read();
                                        }
                                        if(fbp.hie==null){fbp.hie=new HtmlIndentEditor(ma);fbp.addView(fbp.hie,fbp.pageLayoutParams);}
                                        fbp.hie.letText(sb.toString().toCharArray());
                                        //log.alert(sb.toString());
                                        //hie.setBackgroundColor(0xffdec777);
                                        fbp.hie.setVisibility(View.VISIBLE);
                                    }catch(Exception e){
                                        log.alert(e.toString());
                                    }
                                }else if(type.equals("java")){
                                    JavaEditorTab jet=new JavaEditorTab(ma);
                                    TabData.JavaEditorInfo jei=new TabData.JavaEditorInfo();
                                    jei.src=file;
                                    jet.jei=jei;
                                    jet.setText(file.getName());
                                    jet.setOnClickListener(ma.gui.TabOnClick);

                                    jet.jep=new JavaEditorPage(jet);
                                    ma.tabList.addTabInfo(jei);
                                    ma.tdm.updateSavedTabList(ma.tabList);
                                    try{
                                        FileReader fr=new FileReader(file);
                                        StringBuilder sb=new StringBuilder();
                                        int ch=fr.read();
                                        while(ch>=0){
                                            sb.append((char)ch);
                                            ch=fr.read();
                                        }
                                        com.dev.JavaIndentEditor jie=jet.jep.jie;
                                        jie.letText(sb);
                                    }catch(Exception e){
                                        log.alert(e.toString());
                                    }
                                    ma.gui.tabs.addView(jet,TabBar.tablp);
                                    ma.gui.TabOnClick.onClick(jet);
                                }else if(type.equals("c")){
                                    try{
                                        CEditorTab cet=new CEditorTab(ma);
                                        TabData.CEditor ce=new TabData.CEditor();
                                        ce.src=file;
                                        cet.ce=ce;
                                        cet.setText(file.getName());
                                        cet.setOnClickListener(ma.gui.TabOnClick);

                                        ma.tabList.addTabInfo(ce);
                                        ma.tdm.updateSavedTabList(ma.tabList);
                                        ma.gui.tabs.addView(cet,TabBar.tablp);
                                        ma.gui.TabOnClick.onClick(cet);
                                    }catch(Exception e){log.alert(e.toString());}
                                }else{
                                    ma.queryOpenFile.destination=file;
                                    ma.queryOpenFile.show(fbp);
                                }
                            }
                        }else if(dot>=0){

                            try{
                                Uri uri=FileProvider.getUriForFile(ma,ma.getPackageName()+".fileprovider",file);
                                Intent intent=new Intent(Intent.ACTION_VIEW);
                                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_ACTIVITY_NEW_TASK);
                                String ext=name.substring(dot+1);
                                String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
                                if(mime==null){
                                    intent.setDataAndType(uri,"application/octet-stream");
                                }else{
                                    intent.setDataAndType(uri,mime);//log.alert(mime);
                                }
                                //log.alert(uri.toString());
                                //log.alert("open file in expected local");
                                ma.startActivity(intent);

                            }catch(Exception e){
                                log.alert(e.toString());
                            }
                        }else{
                            try{
                                Uri uri=FileProvider.getUriForFile(ma,"com.remotefile.fileprovider",file);
                                Intent intent=new Intent(Intent.ACTION_VIEW);
                                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setDataAndType(uri,"application/octet-stream");
                                ma.startActivity(intent);
                            }catch(Exception e){
                                log.alert(e.toString());
                            }
						}
                    }else{
                        Toast.makeText(ma,"不可读",0).show();
                    }
                }else if(file.canRead()){
                    if(fbp.indentItem^fbp.ivMeans.isSelected()){
                        if(fbp.willshowOpened[pos]){
                            
                        }else{
                            fbp.willshowOpened[pos]=true;
                            WrapData wd;
                            if(fbp.willshowWds==null){
                                fbp.willshowWds=new WrapData[fbp.willshowLen];
                                File[] list=file.listFiles();
                                wd=new WrapData(list,(byte)(fbp.willshowIndents[pos]+1));

                            }else if(fbp.willshowWds[pos]==null){
                                File[] list=file.listFiles();
                                wd=new WrapData(list,(byte)(fbp.willshowIndents[pos]+1));
                            }else{
                                wd=fbp.willshowWds[pos];
                            }
                            int wdlen=wd.fs.length;
                            if(fbp.willshowSorted==null){
                                int willshowLen_will=fbp.willshowLen+wdlen;
                                File[] willshow_will=new File[willshowLen_will];
                                System.arraycopy(fbp.willshow,0,willshow_will,0,pos+1);
                                System.arraycopy(fbp.willshow,pos+1,willshow_will,pos+1+wdlen,fbp.willshowLen-pos-1-wdlen);
                                System.arraycopy(wd.fs,0,willshow_will,pos+1,wdlen);
                                boolean[] willshowOpened_will=new boolean[willshowLen_will];
                                System.arraycopy(fbp.willshowOpened,0,willshowOpened_will,0,pos+1);
                                System.arraycopy(fbp.willshowOpened,pos+1,willshowOpened_will,pos+1+wdlen,fbp.willshowLen-pos-1-wdlen);
                                System.arraycopy(wd.opened,0,willshowOpened_will,pos+1,wdlen);
                                Bitmap[] willshowIcons_will=new Bitmap[willshowLen_will];
                                System.arraycopy(fbp.willshowIcons,0,willshowIcons_will,0,pos+1);
                                System.arraycopy(fbp.willshowIcons,pos+1,willshowIcons_will,pos+1+wdlen,fbp.willshowLen-pos-1-wdlen);
                                System.arraycopy(wd.icons,0,willshowIcons_will,pos+1,wdlen);
                                fbp.willshow=willshow_will;
                                fbp.willshowLen=willshow_will.length;
                            }else{
                                int maped=fbp.willshowSorted[pos];
                                /*TreeNode[] willshow_will=new TreeNode[fbp.willshowLen+ns.length];
                                 System.arraycopy(fbp.willshow,0,willshow_will,0,index+1);
                                 System.arraycopy(fbp.willshow,ptrNow+1+ns.length,willshow_will,ptrNow+1+ns.length,ns.length);
                                 for(int offs=0;offs<ns.length;offs++){
                                 TreeNode tn=ns[offs];
                                 //tn.indent=(byte)ind;
                                 willshow_will[ptrNow+1+offs]=tn;
                                 }
                                 for(int ii=index;ii<fbp.willshowSortedLen;ii++){
                                 int ptr=fbp.willshowSorted[ii];
                                 if(ptr>ptrNow){
                                 fbp.willshowSorted[ii]=ptr+ns.length;
                                 }
                                 }*/

                            }
                            invalidate();
                        }
                    }else{
                        fbp.letDir(file);
                    }
                }else{
                    dir(file);
                }
            }
        }else if(fbp.selected[pos]){
            fbp.selected[pos]=false;
            for(int i=0;true;i++){
                if(i<fbp.selected.length){
                    if(fbp.selected[i]){
                        break;
                    }
                }else{
                    fbp.selected=null;
                    fbp.box.setVisibility(View.GONE);
                    break;
                }
            }
            invalidate();
        }else{
            fbp.selected[pos]=true;
            invalidate();
        }
    }
    void dir(File file){
        MainActivity ma=fbp.ma;
        File ext=Environment.getExternalStorageDirectory();
        String ext_abs=ext.getAbsolutePath();
        String file_abs=file.getAbsolutePath();
        int index=file_abs.indexOf(ext_abs);
        if(index==0){
            URI ext_uri=ext.toURI();
            URI file_uri=file.toURI();
            URI relaed=ext_uri.relativize(file_uri);
            String file_rela_encoded=Uri.encode(relaed.getPath());
            Uri uri=Uri.parse("content://com.android.externalstorage.documents/tree/primary/"+file_rela_encoded);
            Intent intent=new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
            intent.addFlags(intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
            intent.addFlags(intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
            DocumentFile df=DocumentFile.fromTreeUri(ma,uri);
            if(df!=null){
                intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,df.getUri());
                ma.startActivityForResult(intent,11);
            }else{
                Toast.makeText(ma,"请求权限失败",0).show();
            }

        }else{
            Toast.makeText(ma,"不可读",0).show();
        }
        return;
	}
    void atItemLongClick(int pos){
        MainActivity ma=fbp.ma;
        if(ma.queryActionForFile==null)ma.queryActionForFile=new QueryActionForFile(ma);
        File file;
        if(fbp.willshowSorted==null){
            file=fbp.willshow[pos];
        }else{
            int maped=fbp.willshowSorted[pos];
            file=fbp.willshow[maped];
        }
        ma.queryActionForFile.destination=file;
        ma.queryActionForFile.show(fbp);
    }
    void fixInside(Bitmap bm,Rect from,Rect to,int xx,int yy,int ww,int hh){
        int bw=bm.getWidth();
        int bh=bm.getHeight();
        from.right=bw;
        from.bottom=bh;
        int bhWill=bh*ww/bw;
        if(bhWill>hh){
            int bwWill=bw*hh/bh;
            to.left=xx+(ww-bwWill)/2;
            to.top=yy;
            to.right=to.left+bwWill;
            to.bottom=yy+hh;
        }else{
            to.left=xx;
            to.top=yy+(hh-bhWill)/2;
            to.right=xx+ww;
            to.bottom=to.top+bhWill;
        }
    }
    Point touchDown=new Point();
    Point touchSaved=new Point();
    Point touchSaved1=new Point();
    WaitLongClick wlc;
    int fingerMoved;
    int startXSaved;
    int startYSaved;
    int scrolling;
    int fingerMovedLen;
    int pressed;
    boolean inertiaing;
    long touchTimeSaved;
    float speed;
    volatile int touching;
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int ex=(int)event.getX();
        int ey=(int)event.getY();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                fingerMoved=0;
                inertiaing=false;
                pressed=0;
                positionSaved=position;
                positionPressed=-1;
                startXSaved=startX;
                startYSaved=startY;
                scrolling=0;
                touchDown.x=ex;
                touchDown.y=ey;
                touching=1;
                touchTimeSaved=System.currentTimeMillis();
                touchSaved.x=ex;
                touchSaved.y=ey;
                touchSaved1.x=ex;
                touchSaved1.y=ey;
                if(wlc!=null)wlc.interrupt();
                wlc=new WaitLongClick();
                wlc.start();
                if(fbp.willshow==null)break;
                if(fbp.willshowSorted==null){
                    int ds=fbp.willshowLen;
                    int starty=startY;
                    int vh=getHeight();
                    measurePressed:
                    for(int pos=position;pos<ds&&starty<vh;pos++){
                        int iw=(int)(itemHeight*iconWidth);
                        int xx=startX+(fbp.willshowIndents[pos]*itemIdentLen);
                        int bb=starty+itemHeight;
                        if(ey>=starty&&ey<bb){
                            if(ex>=xx){
                                if(ex<xx+iw){
                                    positionPressed=pos;
                                    pressed=1;
                                }else{
                                    positionPressed=pos;
                                    pressed=2;
                                }
                            }
                            invalidate();
                            break;
                        }
                        starty=bb;
                    }
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                wlc.interrupt();
                pressed=0;
                positionPressed=-1;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                if(event.getPointerCount()==1){
                    if(fingerMoved==0){
                        int dxabs=Math.abs(ex-touchDown.x);
                        int dyabs=Math.abs(ey-touchDown.y);
                        long dt=System.currentTimeMillis()-event.getDownTime();
                        if(dyabs>fingerMovedLen){
                            fingerMoved=1;
                            wlc.interrupt();
                            if(dt>0){
                                speed=(float)(ey-touchDown.y)/dt;
                            }
                        }else if(dxabs>fingerMovedLen){
                            fingerMoved=2;
                            wlc.interrupt();
                            if(dt>0){
                                speed=(float)(ex-touchDown.x)/dt;
                            }
                        }
                        touchTimeSaved=System.currentTimeMillis();
                        touchSaved.x=ex;
                        touchSaved.y=ey;
                    }else if(fingerMoved==1){
                        int dy=ey-touchSaved1.y;
                        int startYWill=startYSaved+dy;
                        startY=startYWill;
                        position=positionSaved;
                        long dt=System.currentTimeMillis()-touchTimeSaved;
                        if(dt>0){
                            speed=((ey-touchSaved.y)/dt+speed)/2;
                        }
                        invalidate();
                        touchTimeSaved=System.currentTimeMillis();
                        touchSaved.x=ex;
                        touchSaved.y=ey;
                    }else if(fingerMoved==2){
                        int dx=ex-touchSaved1.x;
                        int startXWill=startXSaved+dx;
                        long dt=System.currentTimeMillis()-touchTimeSaved;
                        if(dt>0){
                            speed=((ex-touchSaved.x)/dt+speed)/2;
                        }
                        if(startXWill<0){
                            startX=0;
                            startXSaved=0;
                            touchSaved1.x=ex;
                            touchSaved1.y=ey;
                        }else{
                            startX=startXWill;
                        }
                        invalidate();
                        touchTimeSaved=System.currentTimeMillis();
                        touchSaved.x=ex;
                        touchSaved.y=ey;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                touching=0;
                if(fingerMoved==0){
                    if(!wlc.actioned){
                        if(positionPressed>=0){
                            if(pressed==1){
                                atIconClick(positionPressed);
                            }else if(pressed==2){
                                atItemClick(positionPressed);
                            }
                        }
                    }
                }else if(fingerMoved==1){
                    inertiaing=true;
                }else if(fingerMoved==2){
                    inertiaing=true;
                }
                positionPressed=-1;
                break;
            case MotionEvent.ACTION_CANCEL:
                touching=0;
                inertiaing=false;
                wlc.interrupt();
                positionPressed=-1;
        }
        return true;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        int vw=getWidth();
        int vh=getHeight();
        Paint paint=new Paint();
        Paint divider=new Paint();
        divider.setColor(0x44999999);
        Rect to=new Rect();
        Rect from=new Rect();
        boolean[] selected=fbp.selected;
        paint.setTextSize(nameTextSize);
        paint.setTypeface(Typeface.MONOSPACE);
        nameTextBaseline=(int)-paint.ascent();
        paint.setTextSize(infoTextSize);
        from.left=0;
        from.top=0;
        infoTextBaseline=(int)-paint.ascent();
        int nameHeight=itemHeight*4/7;
        int pos=position;
        int starty=startY;
        if(fbp.willshow==null){
            paint.setTextSize((int)(30*fbp.ma.dp));
            int tx=(int)(12*fbp.ma.dp);
            int ty=(int)(10*fbp.ma.dp);
            canvas.drawText("Data is null",tx,ty+paint.getTextSize(),paint);
        }else if(fbp.willshowSorted==null){
            int ds=fbp.willshowLen;//int startx=startX;
            //Paint mark=new Paint();
            //mark.setColor(0xffdec777);
            if(ds*itemHeight<vh){
                starty=0;
                startY=0;
                pos=0;
                position=0;
            }else{
                int items=(starty+itemHeight-1)/itemHeight;
                if(pos-items>=0){
                    int end=starty+(ds-pos)*itemHeight;
                    if(end<vh){
                        starty+=vh-end;
                        items=(starty+itemHeight-1)/itemHeight;
                        pos-=items;
                        position=pos;
                        starty-=items*itemHeight;
                        startY=starty;
                    }else{
                        pos-=items;
                        position=pos;
                        starty-=items*itemHeight;
                        startY=starty;
                    }

                }else{
                    pos=0;
                    position=0;
                    starty=0;
                    startY=0;
                }
            }
            String[] names=fbp.names;
            int attrs[]=fbp.willshowAttr;
            drawRaw:
            for(;pos<ds&&starty<vh;pos++){
                int ident=fbp.willshowIndents[pos]*itemIdentLen;
                int xx=startX+ident;
                int iw=(int)(itemHeight*iconWidth);
                File file=fbp.willshow[pos];
                String name=names[pos];
                if(selected==null){
                    if(positionPressed==pos){
                        paint.setColor(0x77999999);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }
                }else{
                    if(selected[pos]){
                        paint.setColor(0x77ffff00);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }else if(positionPressed==pos){
                        paint.setColor(0x77999999);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }
                }
                if(name.charAt(0)=='.'){
                    paint.setColorFilter(fbp.hidden);
                }else{
                    paint.setColorFilter(null);
                }
                int tx=xx+iw+itemTextPaddingLeft;
                int ty1=starty+(nameHeight-nameTextSize)/2+nameTextBaseline;
                int ty2=starty+nameHeight+(itemHeight-nameHeight-infoTextSize)/2+infoTextBaseline;
                paint.setTextSize(nameTextSize);
                paint.setColor(0xff000000);
                canvas.drawText(name,tx,ty1,paint);
                paint.setTextSize(infoTextSize);
                int attr=attrs[pos];
                if((attr&0x10000000)==0x10000000){
                    if(fbp.willshowIcons[pos]==null){
                        if(file.isDirectory()){
                            fixInside(fbp.folder,from,to,xx,starty,iw,itemHeight);
                            canvas.drawBitmap(fbp.folder,from,to,paint);
                           /* if(file.canRead()){
                                paint.setColor(infoTextColor);
                                File list[]=file.listFiles();
                                int count=list.length;
                                if(count==0){
                                    canvas.drawText("空目录",tx,ty2,paint);
                                }else{
                                    canvas.drawText("总有"+count+"项",tx,ty2,paint);
                                }
                            }else{
                                paint.setColor(0xffff0000);
                                canvas.drawText("不可读",tx,ty2,paint);
                            }*/
                        }else{
                            /*long length=file.length();
                            paint.setColor(infoTextColor);
                            if(length>1024*1024){
                                canvas.drawText(length/1024/1024+"MB",tx,ty2,paint);
                            }else if(length>1024){
                                canvas.drawText(length/1024+"KB",tx,ty2,paint);
                            }else{
                                canvas.drawText(length+"B",tx,ty2,paint);
                            }*/
                            if(fbp.willshowIconLoaders[pos]==null){
                                int dot=name.lastIndexOf('.');
                                if(dot<0){
                                    fixInside(fbp.typefile,from,to,xx,starty,iw,itemHeight);
                                    canvas.drawBitmap(fbp.typefile,from,to,paint);
                                    fbp.willshowIcons[pos]=fbp.typefile;
                                }else{
                                    String type=name.substring(dot+1);
                                    if(type.equals("apk")){
                                        fixInside(fbp.typeApk,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeApk,from,to,paint);
                                        LoadApkIcon lai=new LoadApkIcon(fbp,file,pos,itemHeight,false);
                                        fbp.willshowIconLoaders[pos]=lai;
                                        fbp.willshowIcons[pos]=fbp.typeApk;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lai;
                                            lai.start();
                                        }
                                    }else if(type.equals("png")||type.equals("jpg")){
                                        fixInside(fbp.typeImage,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeImage,from,to,paint);
                                        LoadImageIcon lii=new LoadImageIcon(fbp,pos,itemHeight,false);
                                        fbp.willshowIconLoaders[pos]=lii;
                                        fbp.willshowIcons[pos]=fbp.typeImage;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lii;
                                            lii.start();
                                        }
                                    }else if(type.equals("gif")){
                                        fixInside(fbp.typeImage,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeImage,from,to,paint);
                                        LoadGifIcon lgi=new LoadGifIcon(fbp,file,pos);
                                        fbp.willshowIconLoaders[pos]=lgi;
                                        fbp.willshowIcons[pos]=fbp.typeImage;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lgi;
                                            lgi.start();
                                        }
                                    }else if(type.equals("tar")||type.equals("zip")){
                                        fixInside(fbp.typearchive,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typearchive,from,to,paint);
                                        fbp.willshowIcons[pos]=fbp.typearchive;
                                    }else if(type.equals("mp3")||type.equals("m4a")){
                                        fixInside(fbp.typeMusic,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeMusic,from,to,paint);
                                        fbp.willshowIcons[pos]=fbp.typeMusic;
                                    }else{
                                        switch(type){
                                            case "c":
                                                fixInside(fbp.typec,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typec,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typefile;
                                                break;
                                            case "cpp":
                                                fixInside(fbp.typecpp,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typecpp,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typecpp;
                                                break;
                                            case "txt":
                                                fixInside(fbp.typetxt,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typetxt,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typetxt;
                                                break;
                                            case "pdf":
                                                fixInside(fbp.typePdf,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typePdf,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typetxt;
                                                break;
                                            case "java":
                                                fixInside(fbp.typeJava,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeJava,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typeJava;
                                                break;
                                            case "svg":
                                                fixInside(fbp.typeSvg,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeSvg,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typeSvg;
                                                break;
                                            case "html":
                                                fixInside(fbp.typeHtml,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeHtml,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typeHtml;
                                                break;
                                            default:
                                                fixInside(fbp.typefile,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typefile,from,to,paint);
                                                fbp.willshowIcons[pos]=fbp.typefile;
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        fixInside(fbp.willshowIcons[pos],from,to,xx,starty,iw,itemHeight);
                        canvas.drawBitmap(fbp.willshowIcons[pos],from,to,paint);
                        if(file.isDirectory()){
                            if(file.canRead()){
                                paint.setColor(infoTextColor);
                                File list[]=file.listFiles();
                                int count=list.length;
                                if(count==0){
                                    canvas.drawText("空目录",tx,ty2,paint);
                                }else{
                                    canvas.drawText("总有"+count+"项",tx,ty2,paint);
                                }
                            }else{
                                paint.setColor(0xffff0000);
                                canvas.drawText("不可读",tx,ty2,paint);
                            }
                        }else{
                            long length=file.length();
                            paint.setColor(infoTextColor);
                            if(length>1024*1024){
                                canvas.drawText(length/1024/1024+"MB",tx,ty2,paint);
                            }else if(length>1024){
                                canvas.drawText(length/1024+"KB",tx,ty2,paint);
                            }else{
                                canvas.drawText(length+"B",tx,ty2,paint);
                            }
                        }
                    }
                }else{
                    paint.setColor(0xff7f0000);
                    canvas.drawText("不存在",tx,ty2,paint);
                    paint.setColor(0x77888888);
                    paint.setTextSize(nameTextSize);
                    canvas.drawText(name,tx,ty1,paint);
                }
                canvas.drawRect(xx,starty+itemHeight-dividerWidth,vw,starty+itemHeight,divider);
                starty+=itemHeight;
            }//outmost loop
            //}catch(Exception e){log.alert(e.toString(),"pos="+pos);}
        }else{
            int ds=fbp.willshowSortedLen;//int startx=startX;
            //Paint mark=new Paint();
            //mark.setColor(0xffdec777);
            if(ds*itemHeight<vh){
                starty=0;
                startY=0;
                pos=0;
                position=0;
            }else{
                int items=(starty+itemHeight-1)/itemHeight;
                if(pos-items>=0){
                    int end=starty+(ds-pos)*itemHeight;
                    if(end<vh){
                        starty+=vh-end;
                        items=(starty+itemHeight-1)/itemHeight;
                        pos-=items;
                        position=pos;
                        starty-=items*itemHeight;
                        startY=starty;
                    }else{
                        pos-=items;
                        position=pos;
                        starty-=items*itemHeight;
                        startY=starty;
                    }

                }else{
                    pos=0;
                    position=0;
                    starty=0;
                    startY=0;
                }
            }
            String[] names=fbp.names;
            int[] attrs=fbp.willshowAttr;
            drawSorted:
            for(;pos<ds&&starty<vh;pos++){
                int datum=fbp.willshowSorted[pos];
                int ident=fbp.willshowIndents[datum]*itemIdentLen;
                int xx=startX+ident;
                int iw=(int)(itemHeight*iconWidth);
                File file=fbp.willshow[datum];
                String name=names[datum];
                if(selected==null){
                    if(positionPressed==pos){
                        paint.setColor(0x77999999);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }
                }else{
                    if(selected[datum]){
                        paint.setColor(0x77ffff00);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }else if(positionPressed==pos){
                        paint.setColor(0x77999999);
                        canvas.drawRect(xx,starty,vw,starty+itemHeight,paint);
                    }
                }
                if(name.charAt(0)=='.'){
                    paint.setColorFilter(fbp.hidden);
                }else{
                    paint.setColorFilter(null);
                }
                int tx=xx+iw+itemTextPaddingLeft;
                int ty1=starty+(nameHeight-nameTextSize)/2+nameTextBaseline;
                int ty2=starty+nameHeight+(itemHeight-nameHeight-infoTextSize)/2+infoTextBaseline;
                paint.setTextSize(nameTextSize);
                paint.setColor(0xff000000);
                canvas.drawText(name,tx,ty1,paint);
                paint.setTextSize(infoTextSize);
                int attr=attrs[datum];
                if((attr&0x10000000)==0x10000000){
                    if(fbp.willshowIcons[datum]==null){
                        if(file.isDirectory()){
                            fixInside(fbp.folder,from,to,xx,starty,iw,itemHeight);
                            canvas.drawBitmap(fbp.folder,from,to,paint);
                            if(file.canRead()){
                                paint.setColor(infoTextColor);
                                File list[]=file.listFiles();
                                int count=list.length;
                                if(count==0){
                                    canvas.drawText("空目录",tx,ty2,paint);
                                }else{
                                    canvas.drawText("总有"+count+"项",tx,ty2,paint);
                                }
                            }else{
                                paint.setColor(0xffff0000);
                                canvas.drawText("不可读",tx,ty2,paint);
                            }
                        }else{
                            long length=file.length();
                            paint.setColor(infoTextColor);
                            if(length>1024*1024){
                                canvas.drawText(length/1024/1024+"MB",tx,ty2,paint);
                            }else if(length>1024){
                                canvas.drawText(length/1024+"KB",tx,ty2,paint);
                            }else{
                                canvas.drawText(length+"B",tx,ty2,paint);
                            }
                            if(fbp.willshowIconLoaders[datum]==null){
                                int dot=name.lastIndexOf('.');
                                if(dot<0){
                                    fixInside(fbp.typefile,from,to,xx,starty,iw,itemHeight);
                                    canvas.drawBitmap(fbp.typefile,from,to,paint);
                                    fbp.willshowIcons[datum]=fbp.typefile;
                                }else{
                                    String type=name.substring(dot+1);
                                    if(type.equals("apk")){
                                        fixInside(fbp.typeApk,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeApk,from,to,paint);
                                        LoadApkIcon lai=new LoadApkIcon(fbp,file,datum,itemHeight,false);
                                        fbp.willshowIconLoaders[datum]=lai;
                                        fbp.willshowIcons[datum]=fbp.typeApk;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lai;
                                            lai.start();
                                        }
                                    }else if(type.equals("png")||type.equals("jpg")){
                                        fixInside(fbp.typeImage,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeImage,from,to,paint);
                                        LoadImageIcon lii=new LoadImageIcon(fbp,datum,itemHeight,false);
                                        fbp.willshowIconLoaders[datum]=lii;
                                        fbp.willshowIcons[datum]=fbp.typeImage;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lii;
                                            lii.start();
                                        }
                                    }else if(type.equals("gif")){
                                        fixInside(fbp.typeImage,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeImage,from,to,paint);
                                        LoadGifIcon lgi=new LoadGifIcon(fbp,file,datum);
                                        fbp.willshowIconLoaders[datum]=lgi;
                                        fbp.willshowIcons[datum]=fbp.typeImage;
                                        if(fbp.iconLoaderNow==null){
                                            fbp.iconLoaderNow=lgi;
                                            lgi.start();
                                        }
                                    }else if(type.equals("tar")||type.equals("zip")){
                                        fixInside(fbp.typearchive,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typearchive,from,to,paint);
                                        fbp.willshowIcons[datum]=fbp.typearchive;
                                    }else if(type.equals("mp3")||type.equals("m4a")){
                                        fixInside(fbp.typeMusic,from,to,xx,starty,iw,itemHeight);
                                        canvas.drawBitmap(fbp.typeMusic,from,to,paint);
                                        fbp.willshowIcons[datum]=fbp.typeMusic;
                                    }else{
                                        switch(type){
                                            case "c":
                                                fixInside(fbp.typec,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typec,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typefile;
                                                break;
                                            case "cpp":
                                                fixInside(fbp.typecpp,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typecpp,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typecpp;
                                                break;
                                            case "txt":
                                                fixInside(fbp.typetxt,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typetxt,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typetxt;
                                                break;
                                            case "pdf":
                                                fixInside(fbp.typePdf,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typePdf,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typetxt;
                                                break;
                                            case "java":
                                                fixInside(fbp.typeJava,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeJava,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typeJava;
                                                break;
                                            case "svg":
                                                fixInside(fbp.typeSvg,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeSvg,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typeSvg;
                                                break;
                                            case "html":
                                                fixInside(fbp.typeHtml,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typeHtml,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typeHtml;
                                                break;
                                            default:
                                                fixInside(fbp.typefile,from,to,xx,starty,iw,itemHeight);
                                                canvas.drawBitmap(fbp.typefile,from,to,paint);
                                                fbp.willshowIcons[datum]=fbp.typefile;
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        fixInside(fbp.willshowIcons[datum],from,to,xx,starty,iw,itemHeight);
                        canvas.drawBitmap(fbp.willshowIcons[datum],from,to,paint);
                        if(file.isDirectory()){
                            if(file.canRead()){
                                paint.setColor(infoTextColor);
                                File list[]=file.listFiles();
                                int count=list.length;
                                if(count==0){
                                    canvas.drawText("空目录",tx,ty2,paint);
                                }else{
                                    canvas.drawText("总有"+count+"项",tx,ty2,paint);
                                }
                            }else{
                                paint.setColor(0xffff0000);
                                canvas.drawText("不可读",tx,ty2,paint);
                            }
                        }else{
                            long length=file.length();
                            paint.setColor(infoTextColor);
                            if(length>1024*1024){
                                canvas.drawText(length/1024/1024+"MB",tx,ty2,paint);
                            }else if(length>1024){
                                canvas.drawText(length/1024+"KB",tx,ty2,paint);
                            }else{
                                canvas.drawText(length+"B",tx,ty2,paint);
                            }
                        }
                    }
                }else{
                    paint.setColor(0xff7f0000);
                    canvas.drawText("不存在",tx,ty2,paint);
                    paint.setColor(0x77888888);
                    paint.setTextSize(nameTextSize);
                    canvas.drawText(name,tx,ty1,paint);
                }
                canvas.drawRect(xx,starty+itemHeight-dividerWidth,vw,starty+itemHeight,divider);
                starty+=itemHeight;
            }//outmost loop
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch(keyCode){
            case KeyEvent.KEYCODE_ENTER:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    class WaitLongClick extends Thread {
        volatile boolean actioned;
        @Override
        public void run() {
            super.run();
            try{
                sleep(longClickLim);
                actioned=true;
                post(new Runnable(){
                    @Override 
                    public void run(){
                        if(positionPressed>=0)atItemLongClick(positionPressed);
                    }
                });
            }catch(InterruptedException e){}
        }
        
    }
}
